package com.sunni.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTool {
    private static DataSource dataSource;

    static{
        dataSource = new ComboPooledDataSource("c3p0test");
    }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            System.out.println(e);
        }

        return connection;
    }

    public static void close(Connection connection, Statement statement,ResultSet resultSet){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                System.out.println(e);
            }

            try {
                statement.close();
            } catch (SQLException e) {
                System.out.println(e);
            }

            try {
                connection.close();
            } catch (SQLException e) {
                System.out.println(e);
            }
        }
    }

}
